/**
 * Created by ajhin on 2/25/2015.
 */
package com.societyGames.robotsExercises
{
import flash.display.DisplayObject;
import flash.display.Sprite;

public class Background extends Sprite
{
  [Embed(source="../../../../../RobotsExercises/assets/BackTile.png")]
  private static var BackTileClass:Class;

  [Embed(source="../../../../../RobotsExercises/assets/FrontTile.png")]
  private static var FrontTileClass:Class;

  private var _spaceWidth:Number;
  private var _spaceHeight:Number;

  public function Background(gridWidth:int, gridHeight:int, spaceWidth:Number, spaceHeight:Number)
  {
    _spaceWidth = spaceWidth;
    _spaceHeight = spaceHeight;
    addTilesTo(this, gridWidth, gridHeight);
  }

  private function addTilesTo(parentSprite:Sprite, gridWidth:int, gridHeight:int):void
  {
    for (var gridY:int = 0; gridY < gridHeight; gridY++)
    {
      for (var gridX:int = 0; gridX < gridWidth; gridX++)
      {
        var tileClass:Class;
        if (gridY < gridHeight - 1)
        {
          tileClass = BackTileClass;
        }
        else
        {
          tileClass = FrontTileClass;
        }
        placeItem(tileClass, gridX, gridY);
      }
    }
  }

  public function placeItem(imageClass:Class, x:int, y:int):void
  {
    var sprite:DisplayObject = new imageClass();
    sprite.x = x * this._spaceWidth;
    sprite.y = y * this._spaceHeight;
    addChild(sprite);
  }
}
}
